package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.TodayBest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author chenjun
 * @date 2021/8/9
 */

@Service
public class TodayBestService {

    @Autowired
    private UserService userService;

    @Autowired
    private RecommendUserService recommendUserService;

    @Autowired
    private UserInfoService userInfoService;

    @Value("${tanhua.sso.default.user}")
    private Long defaultUserId;

    /**
     * 获取今日佳人
     *
     * @param //token
     * @return com.tanhua.server.vo.TodayBest
     * @date 2021/8/9
     */
    public TodayBest queryTodayBast() {
        /*User user = userService.queryUserByToken(token);
        if (null == user) {
            return null;
        }*/

        User user = UserThreadLocal.get();

        TodayBest todayBest = recommendUserService.queryTodayBast(user.getId());
        if (null == todayBest) {
            //设置默认推荐
            todayBest = new TodayBest();
            todayBest.setId(defaultUserId);
            todayBest.setFateValue(80L);
        }

        //补全个人信息
        UserInfo userInfo = userInfoService.queryUserInfoByUserId(todayBest.getId());
        if (null == userInfo) {
            return null;
        }
        todayBest.setAvatar(userInfo.getLogo());
        todayBest.setNickname(userInfo.getNickName());
        todayBest.setTags(StringUtils.split(userInfo.getTags(), ','));
        todayBest.setGender(userInfo.getSex().getValue() == 1 ? "man" : "woman");
        todayBest.setAge(userInfo.getAge());

        return todayBest;
    }

    /**
     * 推荐用户列表
     *
     * @param //token
     * @param queryParam 茶查条件
     * @return com.tanhua.server.vo.PageResult
     * @date 2021/8/10
     */
    public PageResult queryRecommendation( RecommendUserQueryParam queryParam) {
        /*User user = this.userService.queryUserByToken(token);
        if (null == user) {
            return null;
        }*/

        User user = UserThreadLocal.get();

        PageResult pageResult = new PageResult();
        pageResult.setPage(queryParam.getPage());
        pageResult.setPagesize(queryParam.getPagesize());

        PageInfo<RecommendUser> recommendUserPageInfo = this.recommendUserService.queryRecommendUserList(user.getId(), queryParam.getPage(), queryParam.getPagesize());
        List<RecommendUser> records = recommendUserPageInfo.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return pageResult;
        }

        //收集推荐用户的id
        Set<Long> userIds = new HashSet<>();
        for (RecommendUser record : records) {
            userIds.add(record.getUserId());
        }

        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", userIds);


       /* if (StringUtils.isNotEmpty(queryParam.getGender())) {
            queryWrapper.eq("sex", StringUtils.equals(queryParam.getGender(), "man") ? 1 : 2);
        }

        if (queryParam.getAge() != null) {
            queryWrapper.le("age", queryParam.getAge());
        }

        if (StringUtils.isNotEmpty(queryParam.getCity())) {
            queryWrapper.like("city", queryParam.getCity());
        }*/


        List<UserInfo> userInfoList = this.userInfoService.queryUserInfoList(queryWrapper);
        List<TodayBest> todayBests = new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {
            TodayBest todayBest = new TodayBest();
            todayBest.setId(userInfo.getUserId());
            todayBest.setAvatar(userInfo.getLogo());
            todayBest.setNickname(userInfo.getNickName());
            todayBest.setTags(StringUtils.split(userInfo.getTags(), ','));
            todayBest.setGender(userInfo.getSex().getValue() == 1 ? "man" : "woman");
            todayBest.setAge(userInfo.getAge());
            for (RecommendUser record : records) {
                if (record.getUserId().longValue() == userInfo.getUserId().longValue()) {
                    todayBest.setFateValue(Double.valueOf(Math.floor(record.getScore())).longValue());
                    break;
                }
            }
            todayBests.add(todayBest);
        }
        pageResult.setItems(todayBests);
        Collections.sort(todayBests, (o1, o2) -> new Long(o2.getFateValue() - o1.getFateValue()).intValue());
        return pageResult;
    }
}
